package com.yx.shop.web.admin;

import com.yx.shop.common.Const;
import com.yx.shop.entitly.Product;
import com.yx.shop.service.admin.AdminProductService;
import com.yx.shop.service.admin.impl.AdminProductServiceImpl;
import com.yx.shop.utils.DateUtils;
import com.yx.shop.utils.FileUtil;
import com.yx.shop.web.controller.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

/**
 * @Classname AdminProductController
 * @Description TODO:
 * @Date 2021/05/10 19:17
 * @Created by yx
 */
@WebServlet("/admin/product")
@MultipartConfig(maxFileSize = 1024*1024*100,maxRequestSize = 1024*1024*200)
public class AdminProductController extends BaseServlet {
    private AdminProductService adminProductService = new AdminProductServiceImpl();

    /**
     * @param request
     * @param response
     * @return java.lang.String
     * @Author:yx
     * @Date 2021/05/10 19:23
     * @Description:获取全部商品
     **/
    public String getGoodsList(HttpServletRequest request, HttpServletResponse response) {
        List<Product> productList = adminProductService.getAllProduct();
        //存储数据
        request.setAttribute("goodsList", productList);
        //转发
        return Const.FORWORD + "/admin/showGoods.jsp";
    }

    /**
     * @param request
     * @param response
     * @return java.lang.String
     * @Author:yx
     * @Date 2021/05/10 19:48
     * @Description:新增商品
     **/
    public String addGoods(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //创建商品对象
        Product product = new Product();

        //设置图片存储路径
//        String savePath = request.getServletContext().getRealPath("/WEB-INF/upload");
        //测试不在WEB-INF目录下
        String savePath = request.getServletContext().getRealPath("/upload");
        File file = new File(savePath);
        if (!file.exists()) {
            file.mkdir();
        }
        Collection<Part> parts = request.getParts();
        for (Part part : parts) {
            String fileName = part.getSubmittedFileName();
            if (fileName != null) {
                if (fileName.length() > 0) {
                    String newName = FileUtil.newFileName(fileName);
                    String newPath = FileUtil.newFilePath(fileName, savePath);
                    //写出图片--->保存
                    part.write(newPath+File.separator+newName);
                    //生成数据库存储的图片路径
                    //File.separator路径分割符
                    /*//绝对路径
                    String pimage = newPath +File.separator+ newName;
                    product.setPimage(pimage);*/
                    //获取相对路径
//                    String RelativePath ="WEB-INF"+File.separator+"upload"+File.separator+FileUtil.getNewFileRelativePath(fileName)+File.separator+ newName;
                    String RelativePath ="upload"+File.separator+FileUtil.getNewFileRelativePath(fileName)+File.separator+ newName;
                    //赋值
                    product.setPimage(RelativePath);
                }
            } else {
                String pname = request.getParameter("pname");
                Integer tid = Integer.valueOf(request.getParameter("goodsParent"));
                String ptime = request.getParameter("ptime");
                Double pprice = Double.valueOf(request.getParameter("pprice"));
                Double pstate = Double.valueOf(request.getParameter("pstate"));
                String pinfo = request.getParameter("pinfo");

                //赋值
                product.setTid(tid);
                product.setPname(pname);
                product.setPtime(DateUtils.dataToSql(DateUtils.stringToDate(ptime)));
                product.setPprice(pprice);
                product.setPstate(pstate.intValue());
                product.setPinfo(pinfo);
            }
        }
        //调用业务,添加商品
        adminProductService.insertProduct(product);
        //新增商品成功后,重定向,重新获取商品列表
        return Const.REDIRECT + "/admin/product?method=getGoodsList";
    }
}
